import 'dart:typed_data';

import 'package:vector_math/vector_math.dart';
import 'package:vector_math_test/src/test_utils.dart';

import '../common/test_page.dart';

class Aabb3TestPage extends TestPage{
  Aabb3TestPage(super.title) {
    group('Aabb3', () {
      test('Aabb3.fromBuffer', testAabb3ByteBufferInstanciation);
      test('Aabb3.center', testAabb3Center);
      test('Aabb3.copyCenterAndHalfExtents', testAabb3CopyCenterAndHalfExtents);
      test('Aabb3.centerAndHalfExtents', testAabb3setCenterAndHalfExtents);
      test('Aabb3.fromSphere', testAabb3setSphere);
      test('Aabb3.fromRay', testAabb3setRay);
      test('Aabb3.fromTriangle', testAabb3setTriangle);
      test('Aabb3.fromQuad', testAabb3setQuad);
      test('Aabb3.containsAabb3', testAabb3ContainsAabb3);
      test('Aabb3.containsVector3', testAabb3ContainsVector3);
      test('Aabb3.containsTriangle', testAabb3ContainsTriangle);
      test('Aabb3.containsSphere', testAabb3ContainsSphere);
      test('Aabb3.intersectsWithAabb3', testAabb3IntersectionAabb3);
      test('Aabb3.intersectionVector3', testAabb3IntersectionVector3);
      test('Aabb3.intersectionSphere', testAabb3IntersectionSphere);
      test('Aabb3.intersectionTriangle', testIntersectionTriangle);
      test('Aabb3.intersectionPlane', testIntersectionPlane);
      test('Aabb3.hull', testAabb3Hull);
      test('Aabb3.hullPoint', testAabb3HullPoint);
    });
  }

  void testAabb3ByteBufferInstanciation() {
    final buffer = Float32List.fromList([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).buffer;
    final aabb = Aabb3.fromBuffer(buffer, 0);
    final aabbOffest = Aabb3.fromBuffer(buffer, Float32List.bytesPerElement);

    expect(aabb.min, '');
    expect(aabb.max, '');

    expect(aabbOffest.min, '');
    expect(aabbOffest.max, '');
  }

  void testAabb3Center() {
    final aabb = Aabb3.minMax($v3(1.0, 2.0, 4.0), $v3(8.0, 16.0, 32.0));
    final center = aabb.center;

    expect(center.xyz, '');
  }

  void testAabb3CopyCenterAndHalfExtents() {
    final a1 = Aabb3.minMax($v3(10.0, 20.0, 30.0), $v3(20.0, 40.0, 60.0));
    final a2 = Aabb3.minMax($v3(-10.0, -20.0, -30.0), $v3(0.0, 0.0, 0.0));

    final center = Vector3.zero();
    final halfExtents = Vector3.zero();

    a1.copyCenterAndHalfExtents(center, halfExtents);

    relativeTest(center, $v3(15.0, 30.0, 45.0));
    relativeTest(halfExtents, $v3(5.0, 10.0, 15.0));

    a2.copyCenterAndHalfExtents(center, halfExtents);

    relativeTest(center, $v3(-5.0, -10.0, -15.0));
    relativeTest(halfExtents, $v3(5.0, 10.0, 15.0));
  }

  void testAabb3setCenterAndHalfExtents() {
    final a1 = Aabb3.centerAndHalfExtents($v3(0.0, 0.0, 0.0), $v3(10.0, 20.0, 30.0));
    final a2 = Aabb3.centerAndHalfExtents($v3(-10.0, -20.0, -30.0), $v3(10.0, 20.0, 30.0));

    relativeTest(a1.min, $v3(-10.0, -20.0, -30.0));
    relativeTest(a1.max, $v3(10.0, 20.0, 30.0));

    relativeTest(a2.min, $v3(-20.0, -40.0, -60.0));
    relativeTest(a2.max, $v3(0.0, 0.0, 0.0));
  }

  void testAabb3setSphere() {
    final s = Sphere.centerRadius($v3(10.0, 20.0, 30.0), 10.0);
    final a = Aabb3.fromSphere(s);

    expect(a.intersectsWithVector3(a.center), '');
    expect(a.intersectsWithVector3($v3(20.0, 20.0, 30.0)), '');
  }

  void testAabb3setRay() {
    final r = Ray.originDirection($v3(1.0, 2.0, 3.0), $v3(1.0, 5.0, -1.0)..normalize());
    final a = Aabb3.fromRay(r, 0.0, 10.0);

    expect(a.intersectsWithVector3(r.at(0.0)), '');
    expect(a.intersectsWithVector3(r.at(10.0)), '');
  }

  void testAabb3setTriangle() {
    final t = Triangle.points($v3(2.0, 0.0, 0.0), $v3(0.0, 2.0, 0.0), $v3(0.0, 0.0, 2.0));
    final a = Aabb3.fromTriangle(t);

    expect(a.intersectsWithVector3(t.point0), '');
    expect(a.intersectsWithVector3(t.point1), '');
    expect(a.intersectsWithVector3(t.point2), '');
  }

  void testAabb3setQuad() {
    final q = Quad.points($v3(2.0, 0.0, 0.0), $v3(0.0, 2.0, 0.0), $v3(0.0, 0.0, 2.0), $v3(0.0, 0.0, -2.0));
    final a = Aabb3.fromQuad(q);

    expect(a.intersectsWithVector3(q.point0), '');
    expect(a.intersectsWithVector3(q.point1), '');
    expect(a.intersectsWithVector3(q.point2), '');
    expect(a.intersectsWithVector3(q.point3), '');
  }

  void testAabb3ContainsAabb3() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Aabb3.minMax($v3(2.0, 2.0, 2.0), $v3(7.0, 7.0, 7.0));
    final cutting = Aabb3.minMax($v3(0.0, 0.0, 0.0), $v3(5.0, 5.0, 5.0));
    final outside = Aabb3.minMax($v3(10.0, 10.0, 10.0), $v3(20.0, 20.0, 20.0));
    final grandParent = Aabb3.minMax($v3(0.0, 0.0, 0.0), $v3(10.0, 10.0, 10.0));

    expect(parent.containsAabb3(child), '');
    expect(parent.containsAabb3(parent), '');
    expect(parent.containsAabb3(cutting), '');
    expect(parent.containsAabb3(outside), '');
    expect(parent.containsAabb3(grandParent), '');
  }

  void testAabb3ContainsSphere() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Sphere.centerRadius($v3(3.0, 3.0, 3.0), 1.5);
    final cutting = Sphere.centerRadius($v3(0.0, 0.0, 0.0), 6.0);
    final outside = Sphere.centerRadius($v3(-10.0, -10.0, -10.0), 5.0);

    expect(parent.containsSphere(child), '');
    expect(parent.containsSphere(cutting), '');
    expect(parent.containsSphere(outside), '');
  }

  void testAabb3ContainsVector3() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = $v3(7.0, 7.0, 7.0);
    final cutting = $v3(1.0, 2.0, 1.0);
    final outside = $v3(-10.0, 10.0, 10.0);

    expect(parent.containsVector3(child), '');
    expect(parent.containsVector3(cutting), '');
    expect(parent.containsVector3(outside), '');
  }

  void testAabb3ContainsTriangle() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Triangle.points($v3(2.0, 2.0, 2.0), $v3(3.0, 3.0, 3.0), $v3(4.0, 4.0, 4.0));
    final edge = Triangle.points($v3(1.0, 1.0, 1.0), $v3(3.0, 3.0, 3.0), $v3(4.0, 4.0, 4.0));
    final cutting = Triangle.points($v3(2.0, 2.0, 2.0), $v3(3.0, 3.0, 3.0), $v3(14.0, 14.0, 14.0));
    final outside = Triangle.points($v3(0.0, 0.0, 0.0), $v3(-3.0, -3.0, -3.0), $v3(-4.0, -4.0, -4.0));

    expect(parent.containsTriangle(child), '');
    expect(parent.containsTriangle(edge), '');
    expect(parent.containsTriangle(cutting), '');
    expect(parent.containsTriangle(outside), '');
  }

  void testAabb3IntersectionAabb3() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Aabb3.minMax($v3(2.0, 2.0, 2.0), $v3(7.0, 7.0, 7.0));
    final cutting = Aabb3.minMax($v3(0.0, 0.0, 0.0), $v3(5.0, 5.0, 5.0));
    final outside = Aabb3.minMax($v3(10.0, 10.0, 10.0), $v3(20.0, 20.0, 10.0));
    final grandParent = Aabb3.minMax($v3(0.0, 0.0, 0.0), $v3(10.0, 10.0, 10.0));

    final siblingOne = Aabb3.minMax($v3(0.0, 0.0, 0.0), $v3(3.0, 3.0, 3.0));
    final siblingTwo = Aabb3.minMax($v3(3.0, 0.0, 0.0), $v3(6.0, 3.0, 3.0));
    final siblingThree = Aabb3.minMax($v3(3.0, 3.0, 3.0), $v3(6.0, 6.0, 6.0));

    expect(parent.intersectsWithAabb3(child), '');
    expect(child.intersectsWithAabb3(parent), '');

    expect(parent.intersectsWithAabb3(parent), '');

    expect(parent.intersectsWithAabb3(cutting), '');
    expect(cutting.intersectsWithAabb3(parent), '');

    expect(parent.intersectsWithAabb3(outside), '');
    expect(outside.intersectsWithAabb3(parent), '');

    expect(parent.intersectsWithAabb3(grandParent), '');
    expect(grandParent.intersectsWithAabb3(parent), '');

    expect(siblingOne.intersectsWithAabb3(siblingTwo), '');
    expect(siblingOne.intersectsWithAabb3(siblingThree), '');
  }

  void testAabb3IntersectionSphere() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Sphere.centerRadius($v3(3.0, 3.0, 3.0), 1.5);
    final cutting = Sphere.centerRadius($v3(0.0, 0.0, 0.0), 6.0);
    final outside = Sphere.centerRadius($v3(-10.0, -10.0, -10.0), 5.0);

    expect(parent.intersectsWithSphere(child), '');
    expect(parent.intersectsWithSphere(cutting), '');
    expect(parent.intersectsWithSphere(outside), '');
  }

  void testIntersectionTriangle() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = Triangle.points($v3(2.0, 2.0, 2.0), $v3(3.0, 3.0, 3.0), $v3(4.0, 4.0, 4.0));
    final edge = Triangle.points($v3(1.0, 1.0, 1.0), $v3(3.0, 3.0, 3.0), $v3(4.0, 4.0, 4.0));
    final cutting = Triangle.points($v3(2.0, 2.0, 2.0), $v3(3.0, 3.0, 3.0), $v3(14.0, 14.0, 14.0));
    final outside = Triangle.points($v3(0.0, 0.0, 0.0), $v3(-3.0, -3.0, -3.0), $v3(-4.0, -4.0, -4.0));

    expect(parent.intersectsWithTriangle(child), '');
    expect(parent.intersectsWithTriangle(edge), '');
    expect(parent.intersectsWithTriangle(cutting), '');
    expect(parent.intersectsWithTriangle(outside), '');

    // Special tests
    final testAabb = Aabb3.minMax(
        $v3(20.458911895751953, -36.607460021972656, 2.549999952316284),
        $v3(21.017810821533203, -36.192543029785156, 3.049999952316284));
    final testTriangle = Triangle.points($v3(20.5, -36.5, 3.5), $v3(21.5, -36.5, 2.5), $v3(20.5, -36.5, 2.5));
    testAabb.intersectsWithTriangle(testTriangle);

    final aabb = Aabb3.minMax(
        $v3(19.07674217224121, -39.46818161010742, 2.299999952316284),
        $v3(19.40754508972168, -38.9503288269043, 2.799999952316284));
    final triangle4 = Triangle.points($v3(18.5, -39.5, 2.5), $v3(19.5, -39.5, 2.5), $v3(19.5, -38.5, 2.5));
    final triangle4_1 = Triangle.points($v3(19.5, -38.5, 2.5), $v3(19.5, -39.5, 2.5), $v3(18.5, -39.5, 2.5));
    final triangle4_2 = Triangle.points($v3(18.5, -39.5, 2.5), $v3(19.5, -38.5, 2.5), $v3(18.5, -38.5, 2.5));
    final triangle4_3 = Triangle.points($v3(18.5, -38.5, 2.5), $v3(19.5, -38.5, 2.5), $v3(18.5, -39.5, 2.5));

    expect(aabb.intersectsWithTriangle(triangle4), '');
    expect(aabb.intersectsWithTriangle(triangle4_1), '');
    expect(aabb.intersectsWithTriangle(triangle4_2), '');
    expect(aabb.intersectsWithTriangle(triangle4_3), '');
  }

  void testIntersectionPlane() {
    final plane = Plane.normalconstant($v3(1.0, 0.0, 0.0), 10.0);

    final left = Aabb3.minMax($v3(-5.0, -5.0, -5.0), $v3(5.0, 5.0, 5.0));
    final right = Aabb3.minMax($v3(15.0, 15.0, 15.0), $v3(30.0, 30.0, 30.0));
    final intersect = Aabb3.minMax($v3(5.0, 5.0, 5.0), $v3(15.0, 15.0, 15.0));

    left.intersectsWithPlane(plane);
    right.intersectsWithPlane(plane);

    final result = IntersectionResult();

    intersect.intersectsWithPlane(plane, result: result);

    relativeError(result.axis, $v3(1.0, 0.0, 0.0));
    expect(result.depth, result.axis);
  }

  void testAabb3IntersectionVector3() {
    final parent = Aabb3.minMax($v3(1.0, 1.0, 1.0), $v3(8.0, 8.0, 8.0));
    final child = $v3(7.0, 7.0, 7.0);
    final cutting = $v3(1.0, 2.0, 1.0);
    final outside = $v3(-10.0, 10.0, 10.0);

    expect(parent.intersectsWithVector3(child), '');
    expect(parent.intersectsWithVector3(cutting), '');
    expect(parent.intersectsWithVector3(outside), '');
  }

  void testAabb3Hull() {
    final a = Aabb3.minMax($v3(1.0, 1.0, 4.0), $v3(3.0, 4.0, 10.0));
    final b = Aabb3.minMax($v3(3.0, 2.0, 3.0), $v3(6.0, 2.0, 8.0));

    a.hull(b);

    expect(a.min.xyz, a.max.xyz);
  }

  void testAabb3HullPoint() {
    final a = Aabb3.minMax($v3(1.0, 1.0, 4.0), $v3(3.0, 4.0, 10.0));
    final b = $v3(6.0, 2.0, 8.0);

    a.hullPoint(b);

    expect(a.min.xyz, a.max.xyz);

    final c = $v3(6.0, 0.0, 2.0);

    a.hullPoint(c);

    expect(a.min.xyz, a.max.xyz);
  }

}